package com.github.data_manage.dao

import com.blinkfox.fenix.jpa.QueryFenix
import com.github.data_manage.domain.po.Fund
import com.github.data_manage.domain.vo.FundPageParam
import org.springframework.data.domain.Page
import org.springframework.data.jpa.repository.Modifying
import org.springframework.data.jpa.repository.Query
import org.springframework.data.repository.query.Param

/**
 *@author CHN
 *@since 2024-08-10 14:48
 */
interface FundDao : BaseDao<Fund> {

    @QueryFenix(nativeQuery = true)
    fun findFund(@Param("fundPageParam") fundPageParam: FundPageParam): List<Fund>

    @QueryFenix(nativeQuery = true)
    fun countFund(@Param("fundPageParam") fundPageParam: FundPageParam): Long

    fun findByFundName(fundName: String): Fund?

    @Modifying
    @Query("update Fund set companyName = :companyName,companyAddress =:companyAddress where companyId = :companyId")
    fun updateCompanyNameByCompanyId(companyId: Int, companyName: String, companyAddress: String?)

    fun findAllByCompanyId(companyId: Int): List<Fund>

    @Query("select distinct f.fundId from Fund f")
    fun findAllFundIds(): List<Int>

    @Query("select f.fundName from Fund f where f.fundId = :fundId")
    fun findFundNameById(@Param("fundId") fundId: Int): String?


    fun findAllByFundIdIn(fundIds: MutableCollection<Int>): MutableList<Fund>


    @Modifying
    @Query("update Fund set companyId = :mainCompanyId,companyName = :companyName where companyId = :minorCompanyId")
    fun mergeCompany(
        @Param("mainCompanyId") mainCompanyId: Int,
        @Param("companyName") companyName: String,
        @Param("minorCompanyId") minorCompanyId: Int
    )

    @Query("select f.fundCode from Fund f where f.fundId = :fundId")
    fun findFundCode(fundId: Int): String

    @Query(
        "select max(fund_code) from t_fund where fund_code is not null and fund_code like concat(:codePrefix,'%')",
        nativeQuery = true
    )
    fun findMaxFundCode(@Param("codePrefix") codePrefix: String): String?

    @Query("select fund_code from t_fund where fund_code like concat(:codePrefix,'%') and fund_code > :fundCode", nativeQuery = true)
    fun findBiggerFundCodes(@Param("codePrefix") codePrefix: String, @Param("fundCode") fundCode: String): List<String>

    @Modifying
    @Query("update Fund set fundCode = :fundCode where fundId = :fundId")
    fun updateFundCode(@Param("fundCode") fundCode: String, @Param("fundId") fundId: Int)

    @Modifying
    @Query("update Fund set fundCode = :newFundCode where fundCode = :oldFundCode")
    fun updateFundCode(@Param("oldFundCode") oldFundCode: String, @Param("newFundCode") newFundCode: String)

}
